package evocarcassonne.tileseteditor.ui.javafx.controllers;

import evocarcassonne.core.board.Tile;
import evocarcassonne.core.utils.ZoneType;
import evocarcassonne.tileseteditor.managers.TileManager;
import evocarcassonne.tileseteditor.ui.javafx.components.TileCell;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.control.*;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.util.Callback;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;

public class MainController implements Initializable {

//    public void clearConns() {
//        connPanel.removeAll();
//    }

    TileController tileController;


    @FXML
    Button addTileButton;

    @FXML
    Button removeTileButton;

    @FXML
    Button resetTileSetButton;

    @FXML
    ListView tilesList;

    @FXML
    TextArea messagesArea;

    @FXML
    TitledPane tilePane;

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {

//        Image img = new Image("/resources/patterns/misc/field.png");
//        tileView.setImage(img);

        tilesList.setOnMouseClicked(new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent event) {
                System.out.println("clicked on " + tilesList.getSelectionModel().getSelectedItem());
            }
        });

        tilesList.setCellFactory(new Callback<ListView<String>,
                ListCell<String>>() {
            @Override
            public ListCell<String> call(ListView<String> list) {
                return new TileCell();
            }
        }
        );

        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("logg.fxml"));

        try {
            FXMLLoader tileLoader = new FXMLLoader(getClass().getResource("../fxml/TileUI.fxml"));
            AnchorPane tileUI = (AnchorPane) tileLoader.load();
            tileController = tileLoader.getController();
            tilePane.setContent(tileUI);
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }

    }

    @FXML
    public void addTileAction(ActionEvent event) throws IOException {
        TileManager.addTile();
        Tile tile = TileManager.getTile();

        tilesList.getItems().add(String.valueOf(tile.number));


        messagesArea.setText(messagesArea.getText() + "\n" + "CII");

        refresh();

    }

    @FXML
    public void resetTileSetAction(ActionEvent event) throws IOException {
        TileManager.resetSet();
        tilesList.getItems().clear();
    }

    @FXML
    public void removeTileAction(ActionEvent event) throws IOException {

    }

    public void refresh() {

        // ComboBox
//        ZoneType typeNorth = ((ZoneTypeItem) cb_north.getSelectedItem()).getValue();
//        ZoneType typeEast = ((ZoneTypeItem) cb_east.getSelectedItem()).getValue();
//        ZoneType typeSouth = ((ZoneTypeItem) cb_south.getSelectedItem()).getValue();
//        ZoneType typeWest = ((ZoneTypeItem) cb_west.getSelectedItem()).getValue();

        // COnnections
        tileController.clearConns();

        tileController.addConn(0);

//        if (typeNorth == typeEast) {
//            main.addConn(0);
//        }
//
//        if (typeNorth == typeSouth) {
//            main.addConn(1);
//        }
//
//        if (typeNorth == typeWest) {
//            main.addConn(2);
//        }
//
//        if (typeSouth == typeEast) {
//            main.addConn(3);
//        }
//
//        if (typeWest == typeSouth) {
//            main.addConn(4);
//        }
//
//        if (typeWest == typeEast) {
//            main.addConn(5);
//        }
//
//        if (typeNorth == typeEast && typeNorth == typeWest) {
//            main.addConn(6);
//        }
//
//        if (typeNorth == typeEast && typeNorth == typeSouth) {
//            main.addConn(7);
//        }
//
//        if (typeNorth == typeSouth && typeNorth == typeWest) {
//            main.addConn(8);
//        }
//
//        if (typeWest == typeEast && typeSouth == typeWest) {
//            main.addConn(9);
//        }
//
//        if (typeNorth == typeEast && typeNorth == typeWest && typeNorth == typeSouth) {
//            main.addConn(10);
//        }
    }
}